﻿using System;
using System.Collections.Generic;
using System.Text;
using NGCDS.Properties;
using System.IO;

namespace NGCDS {
    class Args {

        private static Settings s = Settings.Default;
        private bool isCheckConfig = false;
        public bool isReadyToGo = false;
        public List<string> sessionList = new List<string>();

        // config
        public string configFile = s.DEFAULT_CONFIG_FILE_NAME;
        public string logFile = s.DEFAULT_LOGGING_FILE_NAME;


        public void TryPrase(string[] args) {
            string arg;
            string errmsg;
            for (int i = 0; i < args.Length; i++) {
                arg = args[i];

                if (arg.Equals("-help") || arg.Equals("-h")) {
                    printUsage();
                    return;
                } else if (arg.Equals("-version")) {
                    printVersion();
                    return;
                } else if (arg.Equals("-v")) {
                    Globe.isVerbose = true;
                } else if (arg.Equals("-log") || arg.Equals("-l")) {
                    logFile = args[i + 1];
                    i++;
                    // 防呆
                    //if (!isFileExists(logFile, "-log -l", out errmsg)) {
                    //    throw new Exception(errmsg);
                    //}
                } else if (arg.Equals("-cfg") || arg.Equals("-c")) {
                    configFile = args[i + 1];
                    i++;
                    // 防呆
                    if (!isFileExists(configFile, "-cfg -c", out errmsg)) {
                        throw new Exception(errmsg);
                    }
                } else if (arg.Equals("-check")) {
                    isCheckConfig = true;
                } else if (arg.StartsWith("-")) {
                    errmsg = "Not support argument " + arg;
                    throw new Exception(errmsg);
                } else {
                    sessionList.Add(arg.ToUpper());
                }
            }

            if (isCheckConfig == true) {
                ConfigManager config = new ConfigManager(configFile);
                Environment.Exit(0);
            }

            if (sessionList.Count == 0) {
                string msg = "未給定 session name ";
                throw new Exception(msg);
            }
            isReadyToGo = true;

        }

        private bool isFileExists(string file, string arg, out string errmsg) {
            if (File.Exists(file)) {
                errmsg = "";
                return true;
            } else {
                errmsg = String.Format("{0} error: file {1} does not Exists.",
                    arg, file);
                return false;
            }
        }

        public void printUsage() {
            Settings s = Settings.Default;
            string config = s.DEFAULT_CONFIG_FILE_NAME;
            string log = s.DEFAULT_LOGGING_FILE_NAME;
            Console.WriteLine();
            Console.WriteLine(" USAGE: NGCDS [options] session1 [session2] [session3]...");
            Console.WriteLine("");
            Console.WriteLine(" where options are:");
            Console.WriteLine("");
            Console.WriteLine(" -l -log <log file>      customize your log file.     DEFAULT = " + config);
            Console.WriteLine(" -c -cfg <config file>   customize your config file.  DEFAULT = " + log);
            Console.WriteLine(" -check                  ONLY check config file. ");
            Console.WriteLine("                         It's helpful if you change config file. CHECK IT! ");
            Console.WriteLine(" -v                      verbose");
            Console.WriteLine(" -version                show version");
            Console.WriteLine(" -h -help                show this message.");
            Environment.Exit(0);
        }

        private void printVersion() {
            CompiledVersion version = new CompiledVersion();
        }
    }
}
